package me.zhengjie.modules.system.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.TeskAndstudent;
import me.zhengjie.modules.system.domain.TeskExerciseSetComplete;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.service.TeskAndstudentService;
import me.zhengjie.modules.system.service.TeskExerciseSetCompleteService;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@Api(tags = "任务试题集")
@RequestMapping("/api/teskExerciseSetComplete")
public class TeskExerciseSetCompleteController {

    private final TeskExerciseSetCompleteService teskExerciseSetCompleteService;

    private final TeskAndstudentService teskAndstudentService;

    private final UserService userService;
    @PostMapping
    @Log("试题完成")
    @ApiOperation("试题完成任务")
    public ResponseEntity<Object> teskExerciseSetComplete(@RequestBody TeskExerciseSetComplete teskExerciseSetComplete) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teskId",teskExerciseSetComplete.getTeskId());
        TeskExerciseSetComplete currentteskExerciseSetComplete = teskExerciseSetCompleteService.getOne(queryWrapper);

        User user = userService.findByName(SecurityUtils.getCurrentUsername());
        TeskAndstudent teskAndstudent = new TeskAndstudent();

        if(currentteskExerciseSetComplete.getId()!=null){
            teskExerciseSetComplete.setId(currentteskExerciseSetComplete.getId());
            teskExerciseSetCompleteService.updateById(teskExerciseSetComplete);

            QueryWrapper queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("teskId",teskExerciseSetComplete.getTeskId());
            queryWrapper1.eq("userId",Math.toIntExact(user.getId()));
            TeskAndstudent teskAndstudent1 = teskAndstudentService.getOne(queryWrapper1);

            teskAndstudent.setTeskId(teskExerciseSetComplete.getTeskId());
            teskAndstudent.setState(teskExerciseSetComplete.getState());
            teskAndstudent.setUserId(Math.toIntExact(user.getId()));
            teskAndstudent.setId(teskAndstudent1.getId());
            
            teskAndstudentService.updateTeskAndstudent(teskAndstudent);
        } else {

            teskExerciseSetCompleteService.save(teskExerciseSetComplete);
//            teskAndstudentService.createTeskAndstudent(teskAndstudent);
            QueryWrapper queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("teskId",teskExerciseSetComplete.getTeskId());
            queryWrapper1.eq("userId",Math.toIntExact(user.getId()));
            TeskAndstudent teskAndstudent1 = teskAndstudentService.getOne(queryWrapper1);

            teskAndstudent.setTeskId(teskExerciseSetComplete.getTeskId());
            teskAndstudent.setState(teskExerciseSetComplete.getState());
            teskAndstudent.setUserId(Math.toIntExact(user.getId()));
            teskAndstudent.setId(teskAndstudent1.getId());

            teskAndstudentService.updateTeskAndstudent(teskAndstudent);
        }
        return ResponseEntity.ok("试题任务完成");
    }
}
